/**
 * @file main.c
 * @brief 示例程序
 * @version 1.0.0
 * @date 2021-03-19
 *
 * @copyright Copyright (c) 2019  Chipintelli Technology Co., Ltd.
 *
 */
#include <stdio.h> 
#include <malloc.h>
#include "ci112x_core_eclic.h"
#include "ci112x_uart.h"

#define RECEIVE_BUFFER_LENGTH           32

unsigned char polling_send_buffer[] = {'U', 'A', 'R', 'T', ' ', 'p', 'o', 'l', 'l', 'i', 'n', 'g', ' ', 's', 'e', 'n', 'd', ' ', 't', 'e', 's', 't', '\r', '\n'};
unsigned char interrupt_send_buffer[] = {'U', 'A', 'R', 'T', ' ', 'i', 'n', 't', 'e', 'r', 'r', 'u', 'p', 't', ' ', 's', 'e', 'n', 'd', ' ', 't', 'e', 's', 't', '\r', '\n'};
unsigned char receive_buffer[RECEIVE_BUFFER_LENGTH] = {0x0};    //用于接收串口数据的buffer
int uart_send_count = 0;
int uart_rcv_count = 0;
int interrupt_rcv_finished = 0;

void UART1_IRQHandler_(void)
{
    /*发送数据*/
    if (UART_MaskIntState(UART1,UART_TXInt))
    {
        while((!(UART1->UARTFlag & (0x1 << 7))))        //如果FIFO未满，就一直写
        {
            if (uart_send_count < sizeof(interrupt_send_buffer)/sizeof(interrupt_send_buffer[0]))   //是否所有数据已发送完成
            {
                UART1->UARTWrDR = interrupt_send_buffer[uart_send_count++];     //数据写入串口
            }
            else
            {
                UART_IntMaskConfig(UART1, UART_TXInt, ENABLE);  //关闭发送中断
                break;
            }
        }
        UART_IntClear(UART1,UART_TXInt);    //清除中断标志
    }

    /*接受数据*/
    if (UART_MaskIntState(UART1,UART_RXInt))
    {
        while (!(UART1->UARTFlag & (0x1 << 6)) && uart_rcv_count < (RECEIVE_BUFFER_LENGTH - 1)) //如果FIFO不为空，就一直读
        {
            receive_buffer[uart_rcv_count] = UART1->UARTRdDR;   //从串口读取数据
            if (receive_buffer[uart_rcv_count] == '\n' || uart_rcv_count >= (RECEIVE_BUFFER_LENGTH - 1))    //接收BUFFER否已满，或收到回车符认为结束
            {
                interrupt_rcv_finished = 1;     //置接收完成标志
                receive_buffer[uart_rcv_count + 1] = '\0';  //添加字符串结束符
                break;
            }
            uart_rcv_count++;
        }
        UART_IntClear(UART1,UART_RXInt);
    }

    UART_IntClear(UART1,UART_AllInt);
}

/**
 * @brief 主函数，进入应用程序的入口
 */
int main(void)
{
    int i=10;
    printf("0x342==0x%08x\n", read_csr(0x342));
    printf("Hello uart!\n");

    //初始化UART1(轮询模式)
    UARTPollingConfig(UART1, UART_BaudRate9600);

    //轮询发送数据    
    for(i=0; i < sizeof(polling_send_buffer)/sizeof(polling_send_buffer[0]); i++)
    {
        UartPollingSenddata(UART1,polling_send_buffer[i]);
    }
    UartPollingSenddone(UART1);

    //轮询接收数据
    for(i=0; (i < 30 && receive_buffer[i-1] != '\n'); i++)
    {
        receive_buffer[i] = UartPollingReceiveData(UART1);
    }
    receive_buffer[i] = '\0';
    printf(receive_buffer);

    //初始化UART1(中断模式)
    __eclic_irq_set_vector(UART1_IRQn, (int32_t)UART1_IRQHandler_);
    eclic_irq_enable(UART1_IRQn);
    UARTInterruptConfig(UART1, UART_BaudRate9600);

    //中断发送
    uart_send_count = 0;
    UART_IntMaskConfig(UART1, UART_TXInt, DISABLE);     //开启发送中断

    //中断接收
    uart_rcv_count = 0;
    while(!interrupt_rcv_finished); //等待中断接收完成
    printf(receive_buffer);

    while(1)
    {
    }
}


